
--[[

Copyright (c) 2011-2014 chukong-inc.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

]]

local ListView = ccui.ListView

function ListView:onEvent(callback)
    self:addEventListener(function(sender, eventType)
        local event = {}
        if eventType == 0 then
            event.name = "ON_SELECTED_ITEM_START"
        else
            event.name = "ON_SELECTED_ITEM_END"
        end
        event.target = sender
        callback(event)
    end)
    return self
end

function ListView:onScroll(callback)
    self:addScrollViewEventListener(function(sender, eventType)
        local event = {}
        if eventType == 0 then
            event.name = "SCROLL_TO_TOP"
        elseif eventType == 1 then
            event.name = "SCROLL_TO_BOTTOM"
        elseif eventType == 2 then
            event.name = "SCROLL_TO_LEFT"
        elseif eventType == 3 then
            event.name = "SCROLL_TO_RIGHT"
        elseif eventType == 4 then
            event.name = "SCROLLING"
        elseif eventType == 5 then
            event.name = "BOUNCE_TOP"
        elseif eventType == 6 then
            event.name = "BOUNCE_BOTTOM"
        elseif eventType == 7 then
            event.name = "BOUNCE_LEFT"
        elseif eventType == 8 then
            event.name = "BOUNCE_RIGHT"
        end
        event.target = sender
        if callback then
            callback(event)
        end
        self:endCallback(event.name)
    end)
    return self
end

function ListView:init(totalNums,direction,itemWidth,itemHeight)
    local viewSize = self:getContentSize()
    if direction == "v" then
        local row = math.floor(viewSize.height / itemHeight)
        local remainder = viewSize.height % itemHeight
        row = (remainder > 0) and (row + 1) or row
        row = (row > totalNums) and totalNums or row
        self:setInitNumber(row)
        self:setPointer(row + 1)
        self:setEndAction("BOUNCE_BOTTOM")
    elseif direction == "h" then
        local col = math.floor(viewSize.width / itemWidth)
        local remainder = viewSize.width % itemWidth
        col = (remainder > 0) and (col + 1) or col
        col = (col > totalNums) and totalNums or col
        self:setInitNumber(col)
        self:setPointer(col + 1)
        self:setEndAction("BOUNCE_RIGHT")
    end

    self:setPointerNumber(1)
    self:setMaxNumber(totalNums)
end

function ListView:setInitNumber(number)
    self._initNumber = number
end

function ListView:getInitNumber()
    return self._initNumber
end

function ListView:setPointer(pointer)
    self._pointer = pointer
end

function ListView:getPointer()
    return self._pointer or 0
end

function ListView:setPointerNumber(num)
    self._pointerNumber = num
end

function ListView:getPointerNumber()
    return self._pointerNumber or 0
end

function ListView:setEndAction(endAction)
    self._endAction = endAction
end

function ListView:getEndAction()
    return self._endAction
end

function ListView:setEndCallback(callback)
    self._endCallback = callback
end

function ListView:setMaxNumber(max)
    self._maxNumber = max
end

function ListView:getMaxNumber()
    return self._maxNumber
end

function ListView:endCallback(action)
    if self:getEndAction() ~= action then return end
    if self:getPointer() > 0 and self:getPointerNumber() > 0 then
        local number = self:getPointer() + (self:getPointerNumber() - 1)
        local endNumber = (number > self:getMaxNumber()) and self:getMaxNumber() or number
        for i = self:getPointer(),endNumber do
            if self._endCallback then
                self._endCallback(i)
            end
            self:setPointer(self:getPointer() + 1)
        end
    end
end

function ListView:createItemShell(size)
    local default_item = ccui.Layout:create()
    default_item:setTouchEnabled(true)
    default_item:setContentSize(size)
    return default_item
end
